package com.ukao.pad.dialog;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.pad.R;
import com.ukao.pad.adapter.ServiceTimeAdapter;
import com.ukao.pad.adapter.VipGroupListAdapter;
import com.ukao.pad.adapter.VipLeverAdapter;
import com.ukao.pad.base.BaseDialogFragment;
import com.ukao.pad.bean.VipGroupBean;
import com.ukao.pad.bean.VipLeverBean;
import com.ukao.pad.bean.serviceTimeBean;
import com.ukao.pad.consts.GlobalConsts;
import com.ukao.pad.eventbus.ChooseCardEvent;
import com.ukao.pad.listener.MyOnItemClickListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 *
 */
public class ChooseDialogFragment extends BaseDialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    @BindView(R.id.edit_name_cancel)
    Button cancelBtn;  //取消
    @BindView(R.id.edit_name_submit)
    Button submitBtn;  //确定
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.choose_card_recycler)
    RecyclerView   mRecycler;
    private String mParam1;
    private String mParam2;
    private Unbinder unbinder;
    public VipLeverBean.ListBean currentLeverSelect;
    private String mTypeEnter = "";  /*进入类型*/
    private ArrayList<VipLeverBean.ListBean> vipLeverData; //vip等级的数据
    private ArrayList<VipGroupBean> vipGroupingData;     //vip分组数据
    private VipLeverAdapter mVipLeverAdapter;
    private VipGroupListAdapter mVipGroupListAdapter;
    private VipGroupBean currentGroupSelect;
    private ArrayList<serviceTimeBean.ListBean> mServiceData;   //上门取件 时间段数据
    private ServiceTimeAdapter mServiceTimeAdapter;
    private serviceTimeBean.ListBean currentServiceSelect;
    private String time;
    private OnFragmentInteractionListener mListener;

    public ChooseDialogFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
                Bundle bundle = getArguments();
                mTypeEnter = bundle.getString("Type");  //获得进入的类型
                switch (mTypeEnter){
                    case  GlobalConsts.VIP_LEVER_ENTER :
                        vipLeverData= getArguments().getParcelableArrayList("dataList");
                        break;
                    case   GlobalConsts.VIP_GROUND_ENTER :
                        vipGroupingData= getArguments().getParcelableArrayList("dataList");
                        break;
                    case  GlobalConsts.CREATE_ORDERS_ENTER:
//                        ArrayList<serviceTimeBean.ListBean>  arrayList=new ArrayList<>();
                        mServiceData=getArguments().getParcelableArrayList("chooseTime");
                        time=getArguments().getString("time");
                        break;
                }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
           View view=inflater.inflate(R.layout.fragment_dialog_choose, container, false);
        unbinder=  ButterKnife.bind(this,view);
        return  view;
    }

    @Override
    protected void initView() {
        mRecycler.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        switch (mTypeEnter) {
            case GlobalConsts.VIP_LEVER_ENTER:
                tvTitle.setText("会员卡");
                mVipLeverAdapter = new VipLeverAdapter(activity, vipLeverData);
                mRecycler.setAdapter(mVipLeverAdapter);
                mVipLeverAdapter.setOnItemClickListener(mOnItemClickListener);
                break;
            case GlobalConsts.VIP_GROUND_ENTER:
                tvTitle.setText("分组");
                mVipGroupListAdapter= new VipGroupListAdapter(activity,vipGroupingData);
                mRecycler.setAdapter(mVipGroupListAdapter);
                mVipGroupListAdapter.setOnItemClickListener(mOnItemClickListener);
                break;
            case GlobalConsts.CREATE_ORDERS_ENTER:
                tvTitle.setText("取件时间段");
                mServiceTimeAdapter= new ServiceTimeAdapter(activity, mServiceData,time);
                mRecycler.setAdapter(mServiceTimeAdapter);
                mServiceTimeAdapter.setOnItemClickListener(mOnItemClickListener);
                break;
        }
    }
    private OnItemClickListener mOnItemClickListener=new MyOnItemClickListener() {
        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            switch (mTypeEnter) {
                case GlobalConsts.VIP_LEVER_ENTER:
                    VipLeverBean.ListBean itemLever = (VipLeverBean.ListBean) o;
                    if (!itemLever.isCheck()) {
                        ChooseDialogFragment.this.currentLeverSelect= itemLever;
                        itemLever.setCheck(true);
                        List<VipLeverBean.ListBean> mData= mVipLeverAdapter.getDatas();
                        for (int j = 0; j < mData.size(); j++) {
                            if (i != j) {
                                mData.get(j).setCheck(false);
                            }
                        }
                        mVipLeverAdapter.notifyDataSetChanged();
                    }
                    break;
                case GlobalConsts.VIP_GROUND_ENTER:
                    VipGroupBean  mItemGroup= (VipGroupBean) o;
                    if (!mItemGroup.isCheck()) {
                        ChooseDialogFragment.this.currentGroupSelect= mItemGroup;
                        mItemGroup.setCheck(true);
                        List<VipGroupBean>     mData= mVipGroupListAdapter.getDatas();
                        for (int j = 0; j < mData.size(); j++) {
                            if (i != j) {
                                mData.get(j).setCheck(false);
                            }
                        }
                        mVipGroupListAdapter.notifyDataSetChanged();
                    }
                    break;
                case GlobalConsts.CREATE_ORDERS_ENTER:  //下单的
                    serviceTimeBean.ListBean mItemService= (serviceTimeBean.ListBean) o;
                    /*&&mItemService.isEnableds()*/
                    if (!mItemService.ischeck()) {
                        ChooseDialogFragment.this.currentServiceSelect= mItemService;
                        mItemService.setIscheck(!mItemService.ischeck());
                        List<serviceTimeBean.ListBean>        mData= mServiceTimeAdapter.getDatas();
                        for (int j = 0; j < mData.size(); j++) {
                            if (i != j) {
                                mData.get(j).setIscheck(false);
                            }
                        }
                        mServiceTimeAdapter.notifyDataSetChanged();
                    }
                    break;
            }

        }
    };
    @OnClick({R.id.choose_card_recycler, R.id.edit_name_cancel, R.id.edit_name_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.edit_name_cancel://取消按钮
                dismiss();
                break;
            case R.id.close_btn:
                dismiss();
                break;
            case R.id.edit_name_submit:  //确定按钮
                if (mTypeEnter.equals(GlobalConsts.VIP_LEVER_ENTER)) {
                    ChooseCardEvent.postHasData(ChooseCardEvent.Message.lever, currentLeverSelect);
                }else  if (mTypeEnter.equals(GlobalConsts.VIP_GROUND_ENTER)){
                    ChooseCardEvent.postHasData(ChooseCardEvent.Message.groud, currentGroupSelect);
                }else{
                    ChooseCardEvent.postHasData(ChooseCardEvent.Message.update, currentServiceSelect);
                }
                dismiss();
                break;
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);  //点击外面不取消
        initView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("DestroyView", "onDestroyView: ");
        unbinder.unbind();
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }
    /**
     * 依附生命周期
     * @param context
     */
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}
